//
//  Dictionary+Extension.swift
//  
//
//  Created by HOA on 2020/12/29.
//

import Foundation

public extension Dictionary where Key == String {
    
    /**
     * keys 排序
     */
    func ascendElements() -> [(key: Key, value: Value)] {
        
        var new: Array<(key: String, value: Value)> = []
        self.forEach { (ke, va) in
            
            var isDictionaryOrArray = false
            if let _ = va as? [String: Any] {
                isDictionaryOrArray = true
            }
            else if let _ = va as? [Any] {
                isDictionaryOrArray = true
            }
            
            if isDictionaryOrArray == false {
                new.append((key: ke, value: va))
            }
            
        }
        
        return new.sorted { (item0, item1) -> Bool in
            item0.key.localizedStandardCompare(item1.key) == .orderedAscending
        }
    }
}
